package com.gzy.blog.blog.dao.impl;

import java.util.Date;

import org.springframework.stereotype.Repository;

import com.gzy.blog.blog.dao.BlogDao;
import com.gzy.blog.blog.entity.Blog;
import com.gzy.blog.common.BaseDao;
import com.gzy.blog.common.PageModel;

@Repository
public class BlogDaoImpl extends BaseDao<Blog, Integer> implements BlogDao {

	public void addBlog(Blog blog) {
		blog.setCreateTime(new Date());
		save(blog);
	}

	public void delete(Blog blog) {
		blog = get(blog.getId());
		delete(blog);
	}

	public void update(Blog blog) {
		update(blog);
	}

	public Blog get(Integer id) {
		return get(id);
	}

	@Override
	public PageModel<Blog> listBlogWithUser(int pageNo, int pageSize) {
		// TODO Auto-generated method stub
		String sql = "select blog.*,user.photo,user.name as username from blog,user where user.id=blog.user_id";
		return pageBySql(pageNo, pageSize, sql);
	}

	@Override
	public Blog getBlogByUserId(Integer userId) {
		// TODO Auto-generated method stub
		return uniqueEntityByHql("FROM Blog WHERE userId=" + userId);
	}

	@Override
	public Blog getBlogByArticleId(Integer id) {
		// TODO Auto-generated method stub
		String sql = "select b.* from blog b,article a where a.user_id=b.user_id and a.id=" + id;
		return uniqueEntityBySql(sql);
	}

	@Override
	public Blog getBlogById(Integer id) {
		// TODO Auto-generated method stub
		return uniqueEntityByHql("FROM Blog WHERE id=?", id);
	}

	@Override
	public PageModel<Blog> listBlogPage(int pageNo, int pageSize) {
		// TODO Auto-generated method stub
		return pageEntityByHql(pageNo, pageSize, "FROM Blog");
	}

}
